package tk.mynoteapp.dev.demo10.aslist;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class AsListApplication {
    private static final Logger logger = LoggerFactory.getLogger(AsListApplication.class);
    public static void main(String[] args) {
        wrong2();
    }

    private static void wrong1() {
        int[] arr = {1,2,3};
        List list = Arrays.asList(arr);
        logger.info("list:{} size:{} class:{}",
                list, list.size(), list.get(0).getClass());
        // list:[[I@532760d8] size:1 class:class [I
    }

    private static void right1() {
        int[] arr = {1,2,3};
        List list = Arrays.stream(arr).boxed().collect(Collectors.toList());
        logger.info("list:{} size:{} class:{}",
                list, list.size(), list.get(0).getClass());
        // list:[1, 2, 3] size:3 class:class java.lang.Integer
    }

    private static void wrong2() {
        String[] arr = {"1","2","3"};
        List list = Arrays.asList(arr);
        arr[1] = "4";
        try {
            // list是Arrays的一个内部类继承了AbstractArrayList但没有
            list.add("5"); // 报错：java.lang.UnsupportedOperationException
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        logger.info("arr:{} list:{}", Arrays.toString(arr), list);
    }
}
